/**
 * 通知系统前端JavaScript工具函数
 */

// 通知工具类
var NoticeUtils = {
    
    /**
     * 获取未读通知数量
     */
    getUnreadCount: function() {
        $.ajax({
            url: ctx + "notice/user/unread/count",
            type: "get",
            success: function(result) {
                if (result.code == web_status.SUCCESS) {
                    // 更新页面上的未读数量显示
                    $('.notice-unread-count').text(result.data);
                }
            }
        });
    },
    
    /**
     * 显示通知弹窗
     */
    showNoticePopup: function() {
        $.modal.open("我的通知", ctx + "notice/user/list", "900px", "600px");
    },
    
    /**
     * 发送通知
     */
    sendNotice: function(title, content, targetType, targetIds, callback) {
        var data = {
            title: title,
            content: content,
            targetType: targetType,
            targetIds: targetIds,
            noticeType: 1, // 系统通知
            priority: 2,   // 中优先级
            sendMethod: 1  // 站内信
        };
        
        $.ajax({
            url: ctx + "notice/send",
            type: "post",
            data: data,
            success: function(result) {
                if (typeof callback === 'function') {
                    callback(result);
                }
            }
        });
    },
    
    /**
     * 使用模板发送通知
     */
    sendNoticeByTemplate: function(templateCode, variables, targetType, targetIds, callback) {
        var data = {
            templateCode: templateCode,
            variables: JSON.stringify(variables),
            targetType: targetType,
            targetIds: targetIds
        };
        
        $.ajax({
            url: ctx + "notice/send/template",
            type: "post",
            data: data,
            success: function(result) {
                if (typeof callback === 'function') {
                    callback(result);
                }
            }
        });
    },
    
    /**
     * 标记通知为已读
     */
    markAsRead: function(receiveId, callback) {
        $.ajax({
            url: ctx + "notice/mark/read/" + receiveId,
            type: "post",
            success: function(result) {
                if (typeof callback === 'function') {
                    callback(result);
                }
            }
        });
    },
    
    /**
     * 批量标记为已读
     */
    batchMarkAsRead: function(receiveIds, callback) {
        $.ajax({
            url: ctx + "notice/mark/read/batch",
            type: "post",
            data: JSON.stringify(receiveIds),
            contentType: "application/json",
            success: function(result) {
                if (typeof callback === 'function') {
                    callback(result);
                }
            }
        });
    },
    
    /**
     * 处理通知
     */
    handleNotice: function(receiveId, handleStatus, handleRemark, callback) {
        var data = {
            handleStatus: handleStatus,
            handleRemark: handleRemark || ''
        };
        
        $.ajax({
            url: ctx + "notice/handle/" + receiveId,
            type: "post",
            data: data,
            success: function(result) {
                if (typeof callback === 'function') {
                    callback(result);
                }
            }
        });
    },
    
    /**
     * 获取通知详情
     */
    getNoticeDetail: function(receiveId, callback) {
        $.ajax({
            url: ctx + "notice/view/" + receiveId,
            type: "get",
            success: function(result) {
                if (typeof callback === 'function') {
                    callback(result);
                }
            }
        });
    },
    
    /**
     * 获取用户通知列表
     */
    getUserNoticeList: function(params, callback) {
        $.ajax({
            url: ctx + "notice/user/list",
            type: "get",
            data: params,
            success: function(result) {
                if (typeof callback === 'function') {
                    callback(result);
                }
            }
        });
    },
    
    /**
     * 格式化通知内容
     */
    formatNoticeContent: function(content, variables) {
        if (!variables) return content;
        
        for (var key in variables) {
            if (variables.hasOwnProperty(key)) {
                var regex = new RegExp('\\{' + key + '\\}', 'g');
                content = content.replace(regex, variables[key]);
            }
        }
        return content;
    },
    
    /**
     * 显示通知提醒
     */
    showNotification: function(title, content, type) {
        var icon = 'fa fa-bell';
        var bgColor = '#1ab394';
        
        switch(type) {
            case 'success':
                icon = 'fa fa-check-circle';
                bgColor = '#1c84c6';
                break;
            case 'warning':
                icon = 'fa fa-exclamation-triangle';
                bgColor = '#f8ac59';
                break;
            case 'error':
                icon = 'fa fa-times-circle';
                bgColor = '#ed5565';
                break;
        }
        
        $.notify({
            icon: icon,
            title: title,
            message: content
        }, {
            type: type || 'info',
            delay: 5000,
            placement: {
                from: "top",
                align: "right"
            },
            animate: {
                enter: 'animated bounceIn',
                exit: 'animated bounceOut'
            }
        });
    }
};

// 页面加载完成后初始化通知系统
$(document).ready(function() {
    // 每5分钟检查一次未读通知
    setInterval(function() {
        NoticeUtils.getUnreadCount();
    }, 300000);
    
    // 初始化未读数量
    NoticeUtils.getUnreadCount();
});

// 全局通知快捷操作
window.Notice = {
    // 发送通知
    send: function(title, content, targetType, targetIds) {
        return new Promise(function(resolve, reject) {
            NoticeUtils.sendNotice(title, content, targetType, targetIds, function(result) {
                if (result.code == web_status.SUCCESS) {
                    resolve(result);
                } else {
                    reject(result);
                }
            });
        });
    },
    
    // 使用模板发送
    sendByTemplate: function(templateCode, variables, targetType, targetIds) {
        return new Promise(function(resolve, reject) {
            NoticeUtils.sendNoticeByTemplate(templateCode, variables, targetType, targetIds, function(result) {
                if (result.code == web_status.SUCCESS) {
                    resolve(result);
                } else {
                    reject(result);
                }
            });
        });
    },
    
    // 显示通知中心
    showCenter: function() {
        NoticeUtils.showNoticePopup();
    },
    
    // 显示通知提醒
    alert: function(title, content, type) {
        NoticeUtils.showNotification(title, content, type);
    }
};